﻿using ACC.Data.Entities.Common;
using ACC.Extensions.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace ACC.Extensions.PCClient.Helper
{
    public static class ClientHelper
    {
        public static void LoadImageSource(string strFilePath, Image img, out double outHeight,out double outWidth)
        {
            if (!File.Exists(strFilePath))
            {
                outWidth = 0;
                outHeight = 0;
                return;
            }
            using (BinaryReader reader = new BinaryReader(File.Open(strFilePath, FileMode.Open)))
            {
                FileInfo fi = new FileInfo(strFilePath);
                byte[] bytes = reader.ReadBytes((int)fi.Length);
                reader.Close();
                Image image = new Image();
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream(bytes);
                bitmapImage.EndInit();
                img.Source = bitmapImage;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                outHeight = bitmapImage.Height;
                outWidth = bitmapImage.Width;
            }
        }

        public static void LoadImageSource(string strFilePath, Image img)
        {
            if (!File.Exists(strFilePath))
            {
                return;
            }
            using (BinaryReader reader = new BinaryReader(File.Open(strFilePath, FileMode.Open)))
            {
                FileInfo fi = new FileInfo(strFilePath);
                byte[] bytes = reader.ReadBytes((int)fi.Length);
                reader.Close();
                Image image = new Image();
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream(bytes);
                bitmapImage.EndInit();
                img.Source = bitmapImage;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            }
        }
        public static List<ClientFailureBit> GetAllGridPicPositonInfo(Canvas csvImg)
        {
            List<ClientFailureBit> lstStatusBit = new List<ClientFailureBit>();
            foreach (UIElement element in csvImg.Children)
            {
                ClientFailureBit gridStautsBit = new ClientFailureBit();
                if (element is TextBox)
                {
                    string strTag =((TextBox)element).Tag.ToString().Trim();
                    string[] arrTag = strTag.Split('|');
                    gridStautsBit.Bit = arrTag[0] == null ? 0:int.Parse(arrTag[0]);
                    gridStautsBit.Name = arrTag[1] == null ? "" : arrTag[1];

                    string strTxt = ((TextBox)element).Text.ToString().Trim();
                    string[] arrSplit= strTxt.Split('|');
                    gridStautsBit.Description = arrSplit[0] == null ? "" : arrSplit[0];
                    gridStautsBit.Location = arrSplit[1] == null ? "" : arrSplit[1];
                    
                    lstStatusBit.Add(gridStautsBit); 
                }
            }
            return lstStatusBit;
        }
    }
}
